package projectwork.shchuangge.cn.com.newproject.weiget;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.adapter.ChoiceViewPagerAdpter;
import projectwork.shchuangge.cn.com.newproject.uitls.Utils;


/**
 * Created by 赵航 on 2017/6/20.
 */


public class ChoiceHandViewPager extends ViewPager {

    private List mList;
    private int  prevousPosition;
    private List<ImageView> mDots = new ArrayList<>();
    private long                mMillis;
    private OnItemCLickListener mOnItemCLickListener;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (mHandler != null) {
                mHandler.removeCallbacksAndMessages(null);
            }

            int currentItem = ChoiceHandViewPager.this.getCurrentItem();

            //在这里可以看到是否有慢慢滑动的效果
            ChoiceHandViewPager.this.setCurrentItem((currentItem + 1) % mList.size(), true);
            mHandler.sendEmptyMessageDelayed(0, 3000);


        }

    };
    private ChoiceViewPagerAdpter mAdpter;
    private int                   mStartX;
    private float                 mY;

    public ChoiceHandViewPager(Context context) {
        this(context, null);


    }

    public ChoiceHandViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        starHandler();

    }

    private void initData() {


        /**
         * 设置viewpager的事件监听
         */
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        mMillis = System.currentTimeMillis();
                        break;
                    case MotionEvent.ACTION_UP:
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }

                        mHandler.sendEmptyMessageDelayed(0, 3000);
                        long timeMillis = System.currentTimeMillis();

                        /**
                         * 当按下抬起时间小于500毫秒,我们就认为这是点击
                         */

                        if ((timeMillis - mMillis) < 500) {

                            if (mOnItemCLickListener != null) {
                                mOnItemCLickListener.OnItemCLick(ChoiceHandViewPager.this.getCurrentItem());
                            }
                        }

                        break;

                    /**
                     *  这个最重要,当我们手指触摸别的控件,或事件者别的控件所拦截时,就会执行该方法,一旦执行这个方法执行了,action_up就不会执行
                     */
                    case MotionEvent.ACTION_CANCEL:
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        mHandler.sendEmptyMessageDelayed(0, 3000);
                        break;
                }


                return false;
            }
        });
    }


    /**
     * 事件分发
     * <p>
     * 主要负责能不能向viewpager传递事件,让不让viewpager华大最后一页的情况下,能连带viewpager进行滑动
     */

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int currentItem = this.getCurrentItem();

        //只有在索引为0的地方并且是右滑操作时，才进行请求不拦截
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //  mStartX = (int) ev.getX();
                mY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int endX = (int) ev.getX();
                float y = ev.getY();
                //                                  这里可以判断是不是把最后的滑动事件交给父控件,会产生整体滑动
                // int diffX = endX - mStartX;

                float v = y - mY;
                if (v > 0) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                if (currentItem == (mList.size() - 1) || currentItem == 0) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }

                //                } else {
                //                    getParent().requestDisallowInterceptTouchEvent(false);
                //                }
                break;
        }

        //        if (currentItem != 0) {
        //            getParent().requestDisallowInterceptTouchEvent(true);//请求父控件不拦截触摸事件
        //        } else {
        //            getParent().requestDisallowInterceptTouchEvent(false);//父控件想拦截就拦截
        //        }

        //        getParent().requestDisallowInterceptTouchEvent(true);//请求父控件不拦截触摸事件
        //        getParent().requestDisallowInterceptTouchEvent(false);//父控件想拦截就拦截
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 首先创建小圆点
     * 再进行跟随viewpager滑动进行修改
     */

    public void setLinearLayout(LinearLayout linearLayout) {

        for (int i = 0; i < mList.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            ImageView imageView = new ImageView(getContext());
            if (i != 0) {
                params.leftMargin = 12;
                imageView.setEnabled(false);

            }
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(R.drawable.point_viewpager_background);

            linearLayout.addView(imageView);
            mDots.add(imageView);
        }
        ChoiceHandViewPager.this.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mDots.get(prevousPosition).setEnabled(false);
                prevousPosition = position;
                //标题修改与圆点选中

                mDots.get(position).setEnabled(true);


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void starHandler() {
        this.mHandler.sendEmptyMessageDelayed(0, 3000);
    }

    public interface OnItemCLickListener {
        public void OnItemCLick(int position);
    }

    public void setOnItemCLickListener(OnItemCLickListener listener) {
        this.mOnItemCLickListener = listener;
    }

    /**
     * 在这里获取数据设置数据
     */

    public void getImages(List list) {
        this.mList = list;
        mAdpter = new ChoiceViewPagerAdpter(mList);
        initData();
        ChoiceHandViewPager.this.setAdapter(mAdpter);

        Utils.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                mHandler.sendEmptyMessageDelayed(0, 3000);
            }
        });


    }

    /**
     * 向外部提供一个方法进行控制轮播图
     */
    public void stopHandler() {
        mHandler.removeCallbacksAndMessages(null);
    }

}
